`
https://leetcode.cn/problems/smallest-substring-with-identical-characters-ii/
`

/**
 * @param {string} s
 * @param {number} numOps
 * @return {number}
 */
var minLength = function (s, numOps) {
  const n = s.length

  // 能否在 numOps 次操作内，把每段连续相同子串的长度都变成 <= m
  // 思路：https://leetcode.cn/problems/smallest-substring-with-identical-characters-ii/solutions/3027031/er-fen-da-an-tan-xin-gou-zao-pythonjavac-3i4f/
  const check = (m) => {
    let cnt = 0
    if (m === 1) {
      // 改成 0101...
      for (let i = 0; i < n; i++) {
        // 如果 s[i] 和 i 的奇偶性不同，cnt 加一
        cnt += (s[i] ^ i) & 1
      }
      // n - cnt 表示改成 1010...
      cnt = Math.min(cnt, n - cnt)
    } else {
      let k = 0
      for (let i = 0; i < n; i++) {
        k++
        // 到达连续相同子串的末尾
        if (i === n - 1 || s[i] !== s[i + 1]) {
          cnt += Math.floor(k / (m + 1))
          k = 0
        }
      }
    }
    return cnt <= numOps
  }

  let left = 0, right = n
  while (left + 1 < right) {
    const mid = left + Math.floor((right - left) / 2)
    if (check(mid)) {
      right = mid
    } else {
      left = mid
    }
  }
  return right
};